package com.agou.ssh.web;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.agou.app.AppContext;

public class WebUtil {
	
	private static String projectName = "";
	private static String serviceName = "";//
	
	public static final void setProjectName(String name) {
		projectName = name;
	}

	public final static void setServiceName(String name) {
		serviceName = name;
	}

	
	public static final String getProjectName() {
		return projectName;
	}

	public static final String getServiceName() {
		return serviceName;
	}

	public static AppContext getAppContext(HttpServletRequest request) {
		return (AppContext)request.getSession().getAttribute(AppContext.APP_NAME);
	}
	
	public static AppContext getAppContext() {
		return (AppContext) ServletActionContext.getRequest().getSession()
				.getAttribute(AppContext.APP_NAME);
	}

	public static void setAppContext(AppContext ctx) {
		ServletActionContext.getRequest().getSession()
				.setAttribute(AppContext.APP_NAME, ctx);
	}

	public static AppContext createAppContext(String projectName, long userId,String userLoginId, String userName) {
		HttpServletRequest request = ServletActionContext.getRequest();
		AppContext acx = new AppContext();
		acx.sessionId = request.getSession().getId();
		acx.remoteHost = request.getRemoteHost();
		//acx.remoteIp = request.getRemoteAddr();
		acx.remotePort = request.getRemotePort();
		//acx.remoteUser = request.getRemoteUser();
		//acx.serverName = request.getServerName();
		acx.serverPort = request.getServerPort();
		//acx.requestURI = request.getRequestURI();
		acx.servletPath = request.getServletPath();
		//acx.requestURI = request.getRequestURI();
		acx.servletPath = request.getServletPath();
		acx.projectName = projectName;
		acx.userId = userId;
		acx.userLoginId = userLoginId;
		acx.isLogin = true;
		acx.userName = userName;
		ServletActionContext.getRequest().getSession()
				.setAttribute(AppContext.APP_NAME, acx);
		return acx;
	}

}
